﻿using System;
using System.ComponentModel;
using System.Drawing;

namespace HML
{
    /// <summary>
    /// TabItem自定义按钮
    /// </summary>
    [TypeConverter(typeof(PropertyOrderConverter))]
    public class TabItemCustomButton
    {
        #region 新增事件

        public delegate void TabItemCustomButtonEventHandler(object sender, TabItemCustomButttonOperatedEventArgs e);
        private event TabItemCustomButtonEventHandler tabItemCustomButtonClick;
        /// <summary>
        /// TabItem自定义按钮单击事件
        /// </summary>
        [Description("TabItem自定义按钮单击事件")]
        [Category("杂项_选项自定义按钮")]
        public event TabItemCustomButtonEventHandler TabItemCustomButtonClick
        {
            add { this.tabItemCustomButtonClick += value; }
            remove { this.tabItemCustomButtonClick -= value; }
        }

        private event TabItemCustomButtonEventHandler tabItemCustomButtonCheckedChanged;
        /// <summary>
        /// TabItem自定义按钮checked更改事件
        /// </summary>
        [Description("TabItem自定义按钮checked更改事件")]
        [Category("杂项_选项自定义按钮")]
        public event TabItemCustomButtonEventHandler TabItemCustomButtonCheckedChanged
        {
            add { this.tabItemCustomButtonCheckedChanged += value; }
            remove { this.tabItemCustomButtonCheckedChanged -= value; }
        }

        public delegate void TabItemCusomButtonDrawAfterEventHandler(object sender, TabItemCustomButtonDrawAfterEventArgs e);
        private event TabItemCusomButtonDrawAfterEventHandler tabItemCusomButtonDrawAfter;
        /// <summary>
        /// TabItem自定义按钮绘制后事件
        /// </summary>
        [Description("TabItem自定义按钮绘制后事件")]
        [Category("杂项_选项自定义按钮")]
        public event TabItemCusomButtonDrawAfterEventHandler TabItemCusomButtonDrawAfter
        {
            add { this.tabItemCusomButtonDrawAfter += value; }
            remove { this.tabItemCusomButtonDrawAfter -= value; }
        }

        #endregion

        #region 属性

        private string key = "";
        /// <summary>
        /// 按钮Key（必须唯一，可用于索引）
        /// </summary>
        [Description("按钮Key（必须唯一，可用于索引）")]
        [Category("杂项")]
        [PropertyOrder(-138)]
        [DefaultValue("")]
        public string Key
        {
            get { return this.key; }
            set
            {
                if (this.key == value)
                    return;

                this.key = value;
            }
        }

        private bool visible = true;
        /// <summary>
        /// 按钮是否显示
        /// </summary>
        [Description("按钮是否显示")]
        [Category("杂项")]
        [PropertyOrder(-136)]
        [DefaultValue(true)]
        public bool Visible
        {
            get { return this.visible; }
            set
            {
                if (this.visible == value)
                    return;

                this.visible = value;
                this.InitializeInvalidate();
            }
        }

        private bool enabled = true;
        /// <summary>
        /// 按钮使用状态
        /// </summary>
        [Description("按钮使用状态")]
        [Category("杂项")]
        [PropertyOrder(-134)]
        [DefaultValue(true)]
        public bool Enabled
        {
            get { return this.enabled; }
            set
            {
                if (this.enabled == value)
                    return;

                this.enabled = value;
                this.Invalidate();
            }
        }

        private Size size = new Size(14, 14);
        /// <summary>
        /// 按钮Size
        /// </summary>
        [Description("按钮Size")]
        [Category("杂项")]
        [PropertyOrder(-132)]
        [DefaultValue(typeof(Size), "14, 14")]
        public Size Size
        {
            get { return this.size; }
            set
            {
                if (this.size == value || value.Width < 0 || value.Height < 0)
                    return;

                this.size = value;
                this.InitializeInvalidate();
            }
        }

        private LRMargin lRMargin = new LRMargin(2, 2);
        /// <summary>
        /// 按钮左右外边距
        /// </summary>
        [Description("按钮左右外边距")]
        [Category("杂项")]
        [PropertyOrder(-130)]
        [DefaultValue(typeof(LRMargin), "2,2")]
        public LRMargin LRMargin
        {
            get { return this.lRMargin; }
            set
            {
                if (this.lRMargin == value || value.Left < 0 || value.Right < 0)
                    return;

                this.lRMargin = value;
                this.InitializeInvalidate();
            }
        }

        private string tipText = "";
        /// <summary>
        /// 按钮提示文本
        /// </summary>
        [Description("按钮提示文本")]
        [Category("杂项")]
        [PropertyOrder(-128)]
        [DefaultValue("")]
        [Browsable(true)]
        public string TipText
        {
            get { return this.tipText; }
            set
            {
                if (this.tipText == value)
                    return;

                this.tipText = value;
            }
        }

        private Image imageNormal = null;
        /// <summary>
        /// 按钮图片(正常)
        /// </summary>
        [Description("按钮图片(正常)")]
        [PropertyOrder(-126)]
        [DefaultValue(null)]
        [RefreshProperties(RefreshProperties.Repaint)]
        public Image ImageNormal
        {
            get { return this.imageNormal; }
            set
            {
                if (this.imageNormal == value)
                    return;

                this.imageNormal = value;
                this.Invalidate();
            }
        }

        private Image imageEnter = null;
        /// <summary>
        /// 按钮图片(鼠标进入)
        /// </summary>
        [Description("按钮图片(鼠标进入)")]
        [PropertyOrder(-124)]
        [DefaultValue(null)]
        [RefreshProperties(RefreshProperties.Repaint)]
        public Image ImageEnter
        {
            get { return this.imageEnter; }
            set
            {
                if (this.imageEnter == value)
                    return;

                this.imageEnter = value;
                this.Invalidate();
            }
        }

        private bool checkButton = false;
        /// <summary>
        /// 按钮是否为check按钮
        /// </summary>
        [Description("按钮是否为check按钮")]
        [Category("杂项_CheckButton模式")]
        [PropertyOrder(-122)]
        [DefaultValue(false)]
        public bool CheckButton
        {
            get { return this.checkButton; }
            set
            {
                if (this.checkButton == value)
                    return;

                this.checkButton = value;
                this.Invalidate();
            }
        }

        private bool _checked = false;
        /// <summary>
        /// 按钮为check按钮时是否选中
        /// </summary>
        [Description("按钮为check按钮时是否选中")]
        [Category("杂项_CheckButton模式")]
        [PropertyOrder(-120)]
        [DefaultValue(false)]
        public bool Checked
        {
            get { return this._checked; }
            set
            {
                if (this._checked == value)
                    return;

                this._checked = value;

                if (this.owner != null && this.owner.Parent != null && this.owner.Parent is TabControlPlus)
                {
                    TabPagePlus tab_page = (TabPagePlus)this.owner;
                    this.OnTabItemCustomButtonCheckedChanged(new TabItemCustomButttonOperatedEventArgs(tab_page, this));
                }

                this.Invalidate();
            }
        }

        private Image imageCheckedNormal = null;
        /// <summary>
        /// 按钮图片(已选中正常)
        /// </summary>
        [Description("按钮图片(已选中正常)")]
        [Category("杂项_CheckButton模式")]
        [PropertyOrder(-118)]
        [DefaultValue(null)]
        [RefreshProperties(RefreshProperties.Repaint)]
        public Image ImageCheckedNormal
        {
            get { return this.imageCheckedNormal; }
            set
            {
                if (this.imageCheckedNormal == value)
                    return;

                this.imageCheckedNormal = value;
                this.Invalidate();
            }
        }

        private Image imageCheckedEnter = null;
        /// <summary>
        /// 按钮图片(已选中鼠标进入)
        /// </summary>
        [Description("按钮图片(已选中鼠标进入)")]
        [Category("杂项_CheckButton模式")]
        [PropertyOrder(-116)]
        [DefaultValue(null)]
        [RefreshProperties(RefreshProperties.Repaint)]
        public Image ImageCheckedEnter
        {
            get { return this.imageCheckedEnter; }
            set
            {
                if (this.imageCheckedEnter == value)
                    return;

                this.imageCheckedEnter = value;
                this.Invalidate();
            }
        }

        #region 用于临时存储运行信息

        /// <summary>
        /// 按钮rect（包含:外边距、内边距、内容）
        /// </summary>
        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        protected internal RectangleF M_Rect { get; set; }

        /// <summary>
        /// 按钮rect（只包含:内容）
        /// </summary>
        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        protected internal RectangleF C_Rect { get; set; }

        /// <summary>
        /// 自定义保存数据
        /// </summary>
        [DefaultValue(null)]
        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public object Data { get; set; }

        #endregion

        #endregion

        #region 字段

        /// <summary>
        /// TabItem自定义按钮所属TabPage
        /// </summary>
        private TabPagePlus owner;

        #endregion

        public TabItemCustomButton()
        {

        }

        #region 虚方法

        internal protected virtual void OnTabItemCustomButtonClick(TabItemCustomButttonOperatedEventArgs e)
        {
            if (this.tabItemCustomButtonClick != null && this.owner != null && this.owner.Parent != null && this.owner.Parent is TabControlPlus)
            {
                this.tabItemCustomButtonClick(this.owner.Parent, e);
            }
        }

        internal protected virtual void OnTabItemCustomButtonCheckedChanged(TabItemCustomButttonOperatedEventArgs e)
        {
            if (this.tabItemCustomButtonCheckedChanged != null && this.owner != null && this.owner.Parent != null && this.owner.Parent is TabControlPlus)
            {
                this.tabItemCustomButtonCheckedChanged(this.owner.Parent, e);
            }
        }

        internal protected virtual void OnTabItemCusomButtonDrawAfter(TabItemCustomButtonDrawAfterEventArgs e)
        {
            if (this.tabItemCusomButtonDrawAfter != null && this.owner != null && this.owner.Parent != null && this.owner.Parent is TabControlPlus)
            {
                Region source_c_region = e.Graphics.Clip;
                Region item_btn_c_region = source_c_region.Clone();//选项自定义按钮绘图区(C)
                item_btn_c_region.Intersect(e.ClipBounds_C);
                e.Graphics.Clip = item_btn_c_region;

                this.tabItemCusomButtonDrawAfter(this.owner.Parent, e);

                e.Graphics.Clip = source_c_region;
                item_btn_c_region.Dispose();
            }
        }

        #endregion

        #region 私有方法

        /// <summary>
        /// 设置自定义按钮所属的TabPage
        /// </summary>
        /// <param name="owner">设置自定义按钮所属的TabPage</param>
        internal void SetOwner(TabPagePlus owner)
        {
            this.owner = owner;
        }

        /// <summary>
        /// 控件重绘
        /// </summary>
        private void Invalidate()
        {
            if (this.owner != null && this.owner.Parent != null && this.owner.Parent is TabControlPlus)
            {
                TabControlPlus tabControl = (TabControlPlus)this.owner.Parent;
                tabControl.Invalidate();
            }
        }

        /// <summary>
        /// 控件重新初始化和重绘
        /// </summary>
        private void InitializeInvalidate()
        {
            if (this.owner != null && this.owner.Parent != null && this.owner.Parent is TabControlPlus)
            {
                TabControlPlus tabControl = (TabControlPlus)this.owner.Parent;
                tabControl.InitializeRectangle();
                tabControl.Invalidate();
            }
        }

        #endregion

    }

}
